package com.gaussian.property.app.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaussian.property.module.biz.entity.TagLib;
import com.gaussian.property.module.biz.service.ITagLibService;
import com.songqi.common.core.domain.OptionVo;
import com.songqi.common.core.exception.CustomException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/4/18 19:55
 */

@Service
public class TagService {


    @Resource
    private ITagLibService iTagLibService;

    /**
     * 查询我的标签列表
     * @param appUserId
     * @return
     */
    public List<OptionVo> queryUserTagList(String appUserId) {
        List<TagLib> tagLibs = iTagLibService.queryUserTagList(appUserId);
        List<OptionVo> optionVos = new ArrayList<>();
        for (TagLib tagLib : tagLibs) {
            OptionVo optionVo = new OptionVo();
            optionVo.setName(tagLib.getTagName());
            optionVo.setValue(tagLib.getTagLibId());
            optionVos.add(optionVo);
        }
        return optionVos;
    }


    public OptionVo addTag(String appUserId, String tagName) {
        LambdaQueryWrapper<TagLib> queryWrapper = new LambdaQueryWrapper<TagLib>()
                .eq(TagLib::getAppUserId, appUserId)
                .eq(TagLib::getTagName, tagName);
        if(iTagLibService.count(queryWrapper) > 0) {
            throw new CustomException("该标签已存在");
        }
        TagLib tagLib = new TagLib();
        tagLib.setAppUserId(appUserId);
        tagLib.setTagName(tagName);
        tagLib.setTagScope("private");
        iTagLibService.save(tagLib);

        OptionVo optionVo = new OptionVo();
        optionVo.setName(tagLib.getTagName());
        optionVo.setValue(tagLib.getTagLibId());
        return optionVo;
    }



    public void delTag(String tagId) {
        iTagLibService.removeById(tagId);
    }
}
